package com.atwisdom.star.dcs.dao.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atwisdom.star.common.vo.PageInfo;
import com.atwisdom.star.dcs.dao.RealTimeInfoDayDao;
import com.atwisdom.star.dcs.mapper.RealTimeInfoDayMapper;
import com.atwisdom.star.dcs.pojo.RealTimeInfoDay;
import com.atwisdom.star.dcs.util.ConditionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RealTimeInfoDayDaoImpl extends ServiceImpl<RealTimeInfoDayMapper, RealTimeInfoDay> implements RealTimeInfoDayDao {

    /**
     * 根据条件和排序查询符合条件的第一条
     *
     * @Param [condition, sortString]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public RealTimeInfoDay findOne(String condition, String sortString){
        QueryWrapper<RealTimeInfoDay> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition)){
            condition = ConditionUtil.transformOperator(condition);
            queryWrapper.apply(condition);
        }
        ConditionUtil.buildOrderBy(queryWrapper,sortString,"updateTime");
        queryWrapper.last("limit 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询数据
     *
     * @Param
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public List<RealTimeInfoDay> query(String condition){
        QueryWrapper<RealTimeInfoDay> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition)){
            condition = ConditionUtil.transformOperator(condition);
            queryWrapper.apply(condition);
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象id更新数据
     *
     * @Param [RealTimeInfoDay]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public void update(RealTimeInfoDay realTimeInfoDay){
        this.baseMapper.updateById(realTimeInfoDay);
    }

    /**
     * 保存数据
     *
     * @Param [RealTimeInfoDay]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public RealTimeInfoDay insert(RealTimeInfoDay realTimeInfoDay){
        this.baseMapper.insert(realTimeInfoDay);
        return realTimeInfoDay;
    }

    /**
     * 根据对象id删除数据
     *
     * @Param [RealTimeInfoDay]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public void remove(RealTimeInfoDay realTimeInfoDay){
        this.baseMapper.deleteById(realTimeInfoDay);
    }

    /**
     * 根据id数据
     *
     * @Param [id]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public void remove(Object id){
        this.baseMapper.deleteById((Serializable) id);
    }

    /**
     * 根据条件删除数据
     *
     * @Param [condition]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public void remove(String condition){
        QueryWrapper<RealTimeInfoDay> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition)){
            condition = ConditionUtil.transformOperator(condition);
            queryWrapper.apply(condition);
        }
        this.baseMapper.delete(queryWrapper);
    }

    /**
     * 根据id获取数据
     *
     * @Param [id]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public RealTimeInfoDay getById(Object id){
        return this.baseMapper.selectById((Serializable) id);
    }

    /**
     * 查询所有数据
     *
     * @Param []
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public List<RealTimeInfoDay> queryAll(){
        QueryWrapper<RealTimeInfoDay> queryWrapper = new QueryWrapper<>();
        return this.baseMapper.selectList(queryWrapper);
    }
    /**
     * 根据条件分页查询数据
     *
     * @Param [pageNumber, pageSize, condition]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public PageInfo queryByPage(int pageNumber, int pageSize, String condition) {
        Page<RealTimeInfoDay> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<RealTimeInfoDay> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition)){
            condition = ConditionUtil.transformOperator(condition);
            wrapper.apply(condition);
        }
        Page<RealTimeInfoDay> queryPage = this.page(pageInfo, wrapper);
        PageInfo pageResult = PageInfo.of(queryPage);
        return pageResult;
    }
    /**
     * 根据字段名称和值获取数据
     *
     * @Param [key, value]
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public RealTimeInfoDay getByField(String key, String value) {
        QueryWrapper<RealTimeInfoDay> queryWrapper = new QueryWrapper<>();
        String condition = key+"=='"+value+"'";
        if(StringUtils.isNotBlank(condition)){
            condition = ConditionUtil.transformOperator(condition);
            queryWrapper.apply(condition);
        }
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询点位信息
     *
     * @Param []
     * @Author why
     * @Date 2024/7/5
     **/
    @Override
    public List<String> queryPointNames() {
        List<RealTimeInfoDay> realTimeInfos = this.queryAll();
        List<String> names = realTimeInfos.stream().map(r -> r.getName()).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(names)) {
            return names.stream().distinct().collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

}
